package com.lemon.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2006
 * </p>
 * <p>
 * Company:
 * </p>
 * 
 * @author not attributable
 * @version 1.0
 */

public class MD5 {
	public MD5() {
		
	}

	public static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
		}
		return hs.toUpperCase();
	}

	public static String MD5Crypt(String s) {
		 byte[] hash;
	        try {
	            hash = MessageDigest.getInstance("MD5").digest(s.getBytes("UTF-8"));
	        } catch (NoSuchAlgorithmException e) {
	            e.printStackTrace();
	            return null;
	        } catch (UnsupportedEncodingException e) {
	            e.printStackTrace();
	            return null;
	        }

	        StringBuilder hex = new StringBuilder(hash.length * 2);
	        for (byte b : hash) {
	            if ((b & 0xFF) < 0x10)
	                hex.append("0");
	            hex.append(Integer.toHexString(b & 0xFF));
	        }

	        String md5 = hex.toString();
	       // md5 = md5.toUpperCase();
	        return md5;
	}
	
	/**
	 * 返回大写的md5加密串
	 * 
	 * @param string
	 * @return
	 */
	public static String getMD5(String string) {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }

        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10)
                hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }

        String md5 = hex.toString();
		md5 = md5.toUpperCase();
        return md5;
    }

	public static String md5(String string) {
        byte[] hash;
        try {
            hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
        StringBuilder hex = new StringBuilder(hash.length * 2);
        for (byte b : hash) {
            if ((b & 0xFF) < 0x10)
                hex.append("0");
            hex.append(Integer.toHexString(b & 0xFF));
        }

        String md5 = hex.toString();
       // md5 = md5.toUpperCase();
        return md5;
    }
	
	

	public static void main(String[] args) {
//		System.out.println(test.MD5Crypt("12312312"+"rt$^23ERdw4#(64"));
//		System.out.println(test.MD5Crypt("12312312"+"rt$^23ERdw4#(64").substring(0, 15));
//		9cbf8a4dcb8e30682b927f352d6559a0
		// System.out.println(MD5Crypt("123456a"));
		// System.out.println(test.getMD5("123456a"));

		// 注册的时候 A45AF7802E4AEA89D88225CF2B5A2966==1528802469424
		long zhuce = System.currentTimeMillis();

		long time = StringUtil.StringToDate("2018-06-17 23:51:12" , 4).getTime();

		String md5 = getMD5("a12300");
		md5 = getMD5(md5 + time);
		System.out.println(md5);

	}

}